package com.appium.test.core;

import io.appium.java_client.AppiumDriver;

import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.offset.PointOption;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;

import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangkefei
 * @date 2020/2/3 20:16
 */

public class Pub {
    private static AppiumDriver driver;

    public static AppiumDriver getDriver() {
        if(driver==null){
            DesiredCapabilities cap=new DesiredCapabilities();
            //设置测试的web浏览器，如果是测试APP则忽略
            cap.setCapability(CapabilityType.BROWSER_NAME,"");
            //设置测试平台是Android 还是 ios
            cap.setCapability("platformName","Android");
            //设置测试机的ID
            cap.setCapability("deviceName","127.0.0.1:62001");
            //设置安卓机的版本
            cap.setCapability("platformVersion","5.1.1");
            //设置包名 Activity
            cap.setCapability("appPackage","com.tencent.mm");
            cap.setCapability("appActivity","com.tencent.mm.ui.LauncherUI");

            //无法输入字符
            cap.setCapability("unicodeKeyboard", true);
            cap.setCapability("resetKeyboard", true);
            //设置不会充值应用
            cap.setCapability("noReset" ,true);
            //每次启用覆盖session，否则会第二次后报错不能新建session
            cap.setCapability("sessionOverride",true);

            try {
                driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),cap);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        return driver;
    }

    public static WebElement findElement(By by,int time){

        WebElement element = (new WebDriverWait(getDriver(),time))
                .until(ExpectedConditions.presenceOfElementLocated(by));
        return element;
    }
    public static WebElement findElement(By by){
        return findElement(by,2);
    }


    public static List<WebElement> findElements(By by,int time){
        List<WebElement> elements = (new WebDriverWait(getDriver(),time))
                .until(ExpectedConditions.presenceOfAllElementsLocatedBy(by));
        return elements;

    }

    public static void sendText(By by,String text){
        WebElement webText=getDriver().findElement(by);
        webText.click();
        webText.clear();
        webText.sendKeys(text);

    }

    public static boolean isExistElement(By by){
        try {
            getDriver().findElement(by);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    // 上滑
    public static void SwipeUp(int x1,int y1,int x2,int y2) {
        driver=(AndroidDriver)getDriver();

        Dimension size = driver.manage().window().getSize();
        int height = size.height;
        int width = size.width;
        new TouchAction(driver).longPress(PointOption.point(x1, y1))
                .moveTo(PointOption.point(x2, y2)).release()
                .perform();
    }

    // 下滑
    public static void SwipeDown() {
        driver=(AndroidDriver)getDriver();
        Dimension size = driver.manage().window().getSize();
        int height = size.height;
        int width = size.width;
        new TouchAction(driver)
                .longPress(PointOption.point(width / 2, height - 100))
                .moveTo(PointOption.point(width / 2, 100)).release().perform();
    }

    // 左滑
    public void SwipeLeft() {
        driver=(AndroidDriver)getDriver();
        Dimension size = driver.manage().window().getSize();
        int height = size.height;
        int width = size.width;
        new TouchAction(driver)
                .longPress(PointOption.point(width - 100, height / 2))
                .moveTo(PointOption.point(100, height / 2)).release().perform();
    }

    // 右滑
    public void SwipeRight() {
        driver=(AndroidDriver)getDriver();
        Dimension size = driver.manage().window().getSize();
        int height = size.height;
        int width = size.width;
        new TouchAction(driver).longPress(PointOption.point(100, height / 2))
                .moveTo(PointOption.point(width - 100, height / 2)).release()
                .perform();
    }



}
